﻿using Ext.Net;
using NOAH.CMS.Repository;

using System;
using System.Collections.Generic;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class admin_Modules_NewsEvent_News_AddOrEdit : System.Web.UI.Page
{
    private List<NewsCategory> allCategories = new List<NewsCategory>();

    private void initData(int newsArticleId)
    {
        string text4;
        string target;
        List<NewsCategory> parentCategories = new List<NewsCategory>();
        NewsCategoryRepository categoryBiz = new NewsCategoryRepository();
        NewsArticle newsArticle = new NewsArticleRepository().Get(newsArticleId);
        if (newsArticle == null)
        {
            return;
        }
        this.txtTitle.Text = newsArticle.Name;
        this.txtShortContent.Value = newsArticle.ShortContent;
        this.fckHtmlEditor.Value = newsArticle.Content;
        this.txtAuthor.Text = newsArticle.Author;
        this.hdfParentCategoryID.Value = "";
        bool? temp = newsArticle.IsInSlideShow;
        this.chkInSlideShow.Checked = temp.HasValue ? temp.GetValueOrDefault() : false;
        this.triggerFilm.Value = newsArticle.ImageUrl;
        int cateId = newsArticle.CategoryID.Value;
        NewsCategory newsCategory = categoryBiz.Get(cateId);
        this.txtCategory.Text = string.Empty;
        this.hdfParentCategoryID.Value = 0;
        if (newsCategory != null)
        {
            this.txtCategory.Text = newsCategory.Name;
            this.hdfParentCategoryID.Text = cateId.ToString();
        }
        if (newsArticle.Target == null)
        {
        }
        this.cbTarget.SelectedItem.Value = "_self";
        if (newsArticle.Target == null)
        {
        }
        this.cbTarget.SelectedItem.Text = "_self";
        string text3 = newsArticle.Target;
        if (text3 != null)
        {
            target = text3;
        }
        else
        {
            target = "_self";
        }
        string temp1 = target;
        if (temp1 != null)
        {
            if (!(temp1 == "_blank"))
            {
                if (temp1 == "_self")
                {
                    this.cbTarget.SelectedIndex = 0;
                    goto Label_01FE;
                }
            }
            else
            {
                this.cbTarget.SelectedIndex = 1;
                goto Label_01FE;
            }
        }
        this.cbTarget.SelectedIndex = 0;
    Label_01FE:
        text4 = newsArticle.HLink;
        if (text4 == null)
        {
        }
        this.txtLink.Text = "";
        temp = newsArticle.IsHotInSite;
        this.chkIsHotInSite.Checked = temp.HasValue ? temp.GetValueOrDefault() : false;
        temp = newsArticle.IsInSlideShow;
        this.chkInSlideShow.Checked = temp.HasValue ? temp.GetValueOrDefault() : false;
        temp = newsArticle.IsOnTop;
        this.chkIsOnTop.Checked = temp.HasValue ? temp.GetValueOrDefault() : false;
        temp = newsArticle.IsActive;
        this.chkIsActive.Checked = temp.HasValue ? temp.GetValueOrDefault() : false;
    }

    private void initForm()
    {
        this.txtTitle.Text = "";
        this.txtShortContent.Value = "";
        this.fckHtmlEditor.Value = "";
        this.txtAuthor.Text = "";
    }

    public void OnSave(object sender, DirectEventArgs e)
    {
        try
        {

            string mode = e.ExtraParams["mode"] ?? "";

            using (NewsArticleRepository newsArticleBiz = new NewsArticleRepository())
            {
                int newsArticleId = 0;
                newsArticleId = (this.Page.Request.QueryString["Id"] != null) ? int.Parse(this.Page.Request.QueryString["Id"]) : 0;
                NewsArticle newsArticle = null;
                switch (mode)
                {
                    case "add":
                        newsArticle = new NewsArticle
                        {
                            CreatedOn = DateTime.Now,
                            EditedOn = new DateTime?(DateTime.Now),
                            NumberOfView = 0
                        };
                        break;

                    case "edit":
                        newsArticle = newsArticleBiz.Get(newsArticleId);
                        break;
                }
                if (mode != "delete")
                {
                    newsArticle.ImageUrl = Convert.ToString(this.triggerFilm.Value);
                    newsArticle.Name = this.txtTitle.Text;
                    newsArticle.ShortContent = this.txtShortContent.Text;
                    newsArticle.Content = e.ExtraParams["fckParameter"].ToString();
                    newsArticle.Author = this.txtAuthor.Text;
                    newsArticle.HLink = this.txtLink.Text;
                    newsArticle.Target = this.cbTarget.SelectedItem.Value;
                    newsArticle.CategoryID = new int?(int.Parse(this.hdfParentCategoryID.Value.ToString()));
                    newsArticle.IsHotInSite = new bool?(this.chkIsHotInSite.Checked);
                    newsArticle.IsInSlideShow = new bool?(this.chkInSlideShow.Checked);
                    newsArticle.IsOnTop = new bool?(this.chkIsOnTop.Checked);
                    newsArticle.IsActive = new bool?(this.chkIsActive.Checked);
                    newsArticle.IsHotInSite = new bool?(this.chkIsHotInSite.Checked);
                }
                if (mode == "add")
                {
                    newsArticle.CreatedOn = DateTime.Now;
                    newsArticle.EditedOn = new DateTime?(DateTime.Now);
                    newsArticleBiz.Add(newsArticle);
                }
                if (mode == "edit")
                {
                    newsArticle.EditedOn = new DateTime?(DateTime.Now);
                }
                if (mode == "delete")
                {
                    newsArticleBiz.Delete(newsArticleId);
                }

                new NOAH.CMS.Cache.NewsCacheLayer().InvalidateCache();


                Util.ShowMessage(Constants.UPDATE_SUCCESS);
                newsArticleBiz.SubmitChanges();
                ExtNet.Msg.Hide();
                BasePage.RegisterCallBackFromChild(this.ScriptManager2, string.Empty);
                if (mode == "delete" || mode == "edit")
                {
                    BasePage.RegisterCloseWindow(this.ScriptManager2);
                }
            }
        }
        catch (Exception ex)
        {
            Util.ShowMessage2(ex.Message + ex.StackTrace);
            ExtNet.Msg.Hide();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.txtTitle.Focus(true, 50);
        if (!ExtNet.IsAjaxRequest)
        {
            int newsArticleId = 0;
            newsArticleId = (this.Page.Request.QueryString["Id"] != null) ? int.Parse(this.Page.Request.QueryString["Id"]) : 0;
            if (newsArticleId == 0)
            {
                this.btnUpdate.Visible = false;
                this.btnDelete.Visible = false;
                this.initForm();
            }
            else
            {
                this.btnAdd.Visible = false;
                this.btnDelete.Visible = false;
                this.initData(newsArticleId);
            }
        }
    }
}